package test_achieve.h5;

import constant.Constant;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.api.java.zh_cn.那么;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import test_achieve.Hooks;
import util.tools.CommonTools;

/**
 * Created by chenkuo on 2017/7/19.
 */
public class HclientBuniness {
    Login login =new Login();
    OrderMain orderMain = new OrderMain();
    @Given("^h用户已登录：(.+)：(.+)$")
    public void h用户已登录(String name,String pw) throws Throwable {
        login.h已经进入用户登录界面();
        login.h选择密码登录();
        login.h输入用户账户密码(name,pw);
        login.h点击登录按钮();
        orderMain.h点击个人中心图标();
        orderMain.h获取闪送余额();
        //回到下单页面
        CommonTools.back(Constant.driverH,1);
    }
    @那么("^h账户余额正确:(\\d+)$")
    public void h用户账户余额正确(int cost) throws Throwable {
       //从订单进度页回退两次，到 个人中心页
//        Helper.back( Constant.driverH,2);
        //获取当前账户余额
        Float accout = Float.parseFloat(Constant.driverH.findElement(By.xpath("//*[@id=\"subcard-balance\"]/b")).getText());
        System.out.println("下完单后账户余额为："+accout);
        System.out.println("Constant.h5Account-cost 为：" + (Constant.h5Account-cost));
        org.testng.Assert.assertEquals(accout,Constant.h5Account-cost);
    }
    @Then("^h订单信息正确$")
    public void h订单信息正确() throws Throwable {
        //在订单列表页刷新页面后，订单状态变为 闪送完成
        Constant.driverH.navigate().refresh();//刷新当前页面
        Assert.assertTrue(Constant.driverH.findElement(
                By.xpath("//*[@id=\"body\"]/div/ul/li[1]/div/div[1]/div[1]/span[2]")).getText().equals("闪送完成"));
        //进入订单详情 订单状态变为 闪送完成
        Constant.driverH.findElement(By.xpath("//*[@id=\"body\"]/div/ul/li[1]/div/div[1]/div[1]/span[1]")).click();
        Assert.assertTrue(Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[1]/div/span[2]")).getText().equals("闪送完成"));
        //进入订单进度页面 显示 恭喜您的物品已送达!
        Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[1]/a[1]/span")).click();
        Assert.assertTrue(Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[2]/div[1]/div/span[1]")).getText().equals("恭喜您的物品已送达!"));

    }
    @And("^h设置加价金额:(\\d+)$")
    public void h设置加价金额(int addPrice) throws Throwable {
        //点击“加价金额(元)”进入该窗体
//        Constant.driverH.findElement(By.xpath("//*[text()=\"加价金额(元)\"]")).click();

        //显示的加价金额，可以不执行js没实际作用
        String js1="document.getElementsByClassName('active')[0].innerHTML = "+addPrice+";";
        ((JavascriptExecutor)Constant.driverH).executeScript(js1);
//        Thread.sleep(5000);
        //真正起作用的js，把value值设置为加价的金额
        String js2="document.getElementsByClassName('active')[0].setAttribute(\"value\","+addPrice+");";
        ((JavascriptExecutor)Constant.driverH).executeScript(js2);
         //点击确定
        Constant.driverH.findElement(By.xpath("/html/body/div[3]/div[1]/a[2]")).click();
    }
    @And("^h设置订单基本信息:(.+)：(.+)：(.+)：(.+)$")
    public void  h设置订单基本信息(String city,String fAdr,String tAdr,String tel) throws Throwable {
        orderMain.h点击选择城市图标();
        orderMain.h选择城市(city);
        orderMain.h设置收寄件地址信息(fAdr);
        orderMain.h设置收寄件人信息(tel);
        orderMain.h点击确定();
        orderMain.h点击物品送到哪里去();
        orderMain.h设置收寄件地址信息(tAdr);
        orderMain.h设置收寄件人信息(tel);
        orderMain.h点击确定();
    }
    @And("^h设置取件时间:(.+)：(.+)$")
    public void h设置取件时间(String date,String hour) throws Throwable {
        if(!"立即取件".contains(hour)){
        //1 设置取件时间(今天or明天)
        String mydate=CommonTools.getDate(date);
        String js1="document.getElementsByClassName('active')[0].setAttribute(\"value\",'"+mydate+"');";
        ((JavascriptExecutor)Constant.driverH).executeScript(js1);
        //2 设置取件时间(02)
        String js2="document.getElementsByClassName('active')[1].setAttribute(\"value\","+hour+");";
        ((JavascriptExecutor)Constant.driverH).executeScript(js2);
        }
        //点击确定
        Constant.driverH.findElement(By.xpath("/html/body/div[3]/div[1]/a[2]")).click();
    }

    @Then("^h验证加价订单金额:(.+)：(.+)$")
    public void h验证加价订单金额(String price,String addPrice) throws Throwable {
        System.out.println( Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div/div[1]")).getText());
        int finalPrice=Integer.parseInt(price)+Integer.parseInt(addPrice);
        System.out.println("finalPrice = "+finalPrice);
        Assert.assertTrue(
                Constant.driverH.findElement(
                        By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div/div[1]")).getText().
                        contains(String.valueOf(finalPrice))
        );
    }

    @Then("^h取消订单后账户余额正确$")
    public void h取消订单后账户余额正确() throws Throwable {
        //从订单列表页 回退一次 到 个人中心页
        CommonTools.back(Constant.driverH,1);
        Constant.driverH.navigate().refresh();//刷新页面
        Float count =Float.parseFloat(
                Constant.driverH.findElement(By.xpath("//*[@id=\"subcard-balance\"]/b")).getText()
        );
        System.out.println("取消单后账户余额："+count);
        Assert.assertEquals(Constant.h5Account,count,00000.1);

    }
    @And("^h获得订单信息$")
    public void h获得订单信息() throws Throwable {
        //订单号
        String orderN=Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[1]/div/span[1]")).getText();
        //取件信息
        String fInf=Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[1]/div/div[1]")).getText();
        fInf = fInf.replaceAll("\r|\n|,|\\s*", "");//去换行，逗号，空格
        //送件信息
        String tInf=Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[1]/div/div[2]")).getText();
        tInf = tInf.replaceAll("\r|\n|,|\\s*", "");

        //物品类型
        String type= Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[3]/div/div[1]/span[2]")).getText();
        //取件时间                                           //*[@id="iss-app"]/div/div[2]/div[2]/div[1]/div[3]/div[2]/div/div/span[2]
        String time= Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[2]/div/div/span[2]")).getText();
        //备注信息
        String memo= Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[3]/div/div[2]/span[2]")).getText();
        //公斤
        String weight= Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[2]/div/span[3]")).getText();
        Constant.orderDetail.put("orderN",orderN);
        Constant.orderDetail.put("fInf",fInf);
        Constant.orderDetail.put("tInf",tInf);
        Constant.orderDetail.put("type",type);
        Constant.orderDetail.put("time",time);
        Constant.orderDetail.put("memo",memo);
        Constant.orderDetail.put("weight",weight);
//        System.out.println(" 取件信息:"+fInf);
//        System.out.println(" tInf:"+tInf);
//        System.out.println(" type:"+type);
//        System.out.println(" time:"+time);
//        System.out.println(" memo:"+memo);
//        System.out.println(" weight:"+weight);
    }
    @And("^h验证下单信息$")
    public void h验证下单信息() throws Throwable {
        //取件信息
        String fInf=Constant.driverH.findElement(By.cssSelector("div.address-mess.from-addr")).getText();
        fInf = fInf.replaceAll("\\s*|\t|\r|\n", "");
        //送件信息
        String tInf=Constant.driverH.findElement(By.cssSelector("div.address-mess.to-addr")).getText();
        tInf = tInf.replaceAll("\\s*|\t|\r|\n", "");//去回车，换行，空格

        //物品类型
        String type= Constant.driverH.findElement(By.cssSelector("span.weight-info.goods-type-info")).getText();
        //取件时间
        String time= Constant.driverH.findElement(By.cssSelector("span.fetch-time-input")).getText();
        //备注信息
//        String memo= Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[1]/div[5]/div[1]/input")).getAttribute("value");
        //公斤
        String weight= Constant.driverH.findElement(By.cssSelector("span.weight")).getText();
//        System.out.println(" ==fInf:"+fInf);
//        System.out.println(" ==tInf:"+tInf);
//        System.out.println(" ==type:"+type);
//        System.out.println(" ==time："+time);
//        System.out.println(" ==memo:"+memo);
//        System.out.println("==weight:"+weight);
//        System.out.println("=========================");
//        System.out.println(Constant.orderDetail.get("fInf").toString().trim());
        Assert.assertTrue( Constant.orderDetail.get("fInf").toString().trim().equals(fInf));
        Assert.assertTrue( Constant.orderDetail.get("tInf").toString().trim().equals(tInf));
        Assert.assertTrue( Constant.orderDetail.get("type").equals(type));
        Assert.assertTrue( Constant.orderDetail.get("time").equals(time));
//        Assert.assertTrue( Constant.orderDetail.get("memo").equals(memo));
        Assert.assertTrue( Constant.orderDetail.get("weight").toString().contains(weight));

    }
    @And("^h设置物品重量:(\\d+)$")
    public void  h设置物品重量(int addWeight) throws Throwable {
        Thread.sleep(1000);
        //真正起作用的js，把value值设置为加价的金额
        String js2="document.getElementsByClassName('active')[0].setAttribute(\"value\","+addWeight+");";
        ((JavascriptExecutor)Constant.driverH).executeScript(js2);
        //点击确定
        Constant.driverH.findElement(By.xpath("/html/body/div[3]/div[1]/a[2]")).click();
    }
    @Then("^h验证订单金额:(.+)$")
    public void h验证订单金额(String price) throws Throwable {
         String p =Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div/div[1]")).getText();
//        System.out.println("下单页订单价格："+p);
        Assert.assertTrue(
                Constant.driverH.findElement(
                        By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div/div[1]")).getText().contains(price)
        );
    }
    @Then("^h验证支付页订单金额:(.+)$")
    public void h验证支付页订单金额(String price) throws Throwable {
        Thread.sleep(2000);
        String p =Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[3]/div/div[1]")).getText();
        System.out.println("支付页订单价格："+p);
        Assert.assertTrue(p.equals("￥"+price));
    }
    @Then("^h验证订单详情中总价:(.+)$")
    public void h验证订单详情中总价(String price) throws Throwable {
        Thread.sleep(1000);
        String p =Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[2]/div/span[1]")).getText().trim();
        System.out.println("订单详情中总价："+p);
        Assert.assertTrue(p.contains(price));
    }

    @Then("^h验证订单详情中订单总价:(.+)$")
    public void h验证订单详情中订单总价(String price) throws Throwable {
        Thread.sleep(1000);
        String p =Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[5]/div/div[1]/div[1]/span[2]")).getText().trim();
        System.out.println("订h验证订单详情中订单总价："+p);
        Assert.assertTrue(p.contains(price));
    }

    @Then("^h验证订单详情中实付款:(.+)$")
    public void h验证订单详情中实付款(String price) throws Throwable {
        Thread.sleep(1000);
        String p =Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[5]/div/div[4]/span[2]")).getText().trim();
        System.out.println("h验证订单详情中实付款："+p);
        Assert.assertTrue(p.contains(price));
    }
    @Then("^h验证账户余额$")
    public void h验证账户余额() throws Throwable {
        Thread.sleep(2000);
        String p =Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div/div[2]/div[2]")).getText();
        System.out.println("账户余额："+p);
        String account=""+(int)Constant.h5Account;
        System.out.println("账户余额："+account);
        Assert.assertTrue(p.contains(account));
    }
    @Then("^h验证账户余额:(\\d+)$")
    public void h验证账户余额(int price) throws Throwable {
        Thread.sleep(2000);
        String p =Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div/div[2]/div[2]")).getText();
        System.out.println("账户余额："+p);
        String account=""+((int)Constant.h5Account-price);
        System.out.println("账户余额："+account);
        Assert.assertTrue(p.contains(account));
    }
    @Then("^h下单成功$")
    public void h下单成功() throws Throwable {
        Assert.assertTrue(
                Constant.driverH.findElement(
                        By.xpath("//*[@id=\"body\"]/div/div[2]")).getText().equals("支付成功!"));

    }
    @Then("^h取消订单成功$")
    public void h取消订单成功() throws Throwable {
        Thread.sleep(1000);
        Assert.assertTrue( "已取消".equals(
                Constant.driverH.findElement(By.xpath("//*[@id=\"body\"]/div/ul/li[1]/div/div[1]/div[1]/span[2]")).getText())
        );
    }

    @Then("^h全部订单列表正确$")
    public void h全部订单列表正确() throws Throwable {
        String status1= Constant.driverH.findElement(By.xpath("//*[@id=\"body\"]/div/ul/li[1]/div/div[1]/div[1]/span[2]")).getText();
        String status2= Constant.driverH.findElement(By.xpath("//*[@id=\"body\"]/div/ul/li[2]/div/div[1]/div[1]/span[2]")).getText();
        Assert.assertTrue(status1.equals("待支付"));
        Assert.assertTrue(status2.equals("已取消"));
    }
    @Then("^h订单列表正确:(.+)$")
    public void h订单列表正确(String status) throws Throwable {
        Thread.sleep(1000);
        String s= Constant.driverH.findElement(
                By.xpath("//*[@id=\"body\"]/div/ul/li/div/div[1]/div[1]/span[2]")).getText();
//        System.out.println(" s:"+s);
//        System.out.println(" status:"+status);
        Assert.assertTrue(s.equals(status));
    }
    @Then("^h订单详情正确:(.+)$")
    public void h订单详情正确(String status) throws Throwable {
        String s[]=status.split(",");
        String status1= Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[1]/div/span[2]")).getText();
        String button1="";
        String button2="";
        if(s[0].equals("待支付")){
            button1 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button")).getText();
            button2 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/a")).getText();
         }else if (s[0].equals("已取件")){
            button2 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button")).getText();
        }else if(s[0].equals("闪送完成")){
            button1=Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/a[1]")).getText();
            button2 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/a[2]")).getText();
        }else{
            button1=Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button")).getText();
            button2 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button[2]")).getText();
        }
//        System.out.println("button1:"+button1);
//        System.out.println("===s[1]:"+s[1]);
        Assert.assertTrue(status1.equals(s[0]));
        Assert.assertTrue(button1.equals(s[1].trim()));
        Assert.assertTrue(button2.equals(s[2]));
    }
    @Then("^h订单进度正确:(.+)$")
    public void h订单进度正确(String status) throws Throwable {
        String s[]=status.split(",");

        String status1="";
        String button1="";
        String button2="";
        if(s[0].equals("取件成功")){
            status1= Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[2]/div[1]/div/span[1]")).getText();
            button2 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button")).getText();
        }else if(s[0].equals("等待支付")){
            status1= Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div/div/div/span[1]")).getText();
            button1 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button")).getText();
            button2 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/a")).getText();
        }else if(s[0].equals("恭喜您的物品已送达!")){
            status1= Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[2]/div[1]/div/span[1]")).getText();
            button1=Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/a[1]")).getText();
            button2 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/a[2]")).getText();
        }else if(s[0].equals("抢单成功")){
            status1= Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[2]/div[1]/div/span[1]")).getText();
            button1 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button[1]")).getText();
            button2 =Constant.driverH.findElement(
                    By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button[2]")).getText();
        }
//        System.out.println("status1:"+status1);
//        System.out.println("===s[0]:"+s[0]);
        Assert.assertTrue(status1.equals(s[0]));
        Assert.assertTrue(button1.equals(s[1].trim()));
        Assert.assertTrue(button2.equals(s[2]));
    }

    @When("^后台留单$")
    public void 后台留单() throws Throwable {
        Hooks h=new Hooks();
        h.cancelOrder();
        System.out.println("==后台留单成功！");
    }

}
